<template>
	<view class="bg">
		<!-- logo背景图 -->
		<logoBack :back="false"></logoBack>

		<!-- from表单 -->
		<view class="form">
			<view class="formCenter">
				<view class="inputView">
					<view class="label">
						<image src="https://yun.qiongtuyule.com/static/img/login/account.png" class="accountLabel" mode=""></image>
					</view>
					<view class="input">
						<!-- <input placeholder-class="inputPla" type="text" placeholder="请输入账号" /> -->
						<input v-model="account" placeholder-class="inputPla" type="text" placeholder="请输入账号" />

					</view>
				</view>
				<view class="inputView">
					<view class="label">
						<image src="https://yun.qiongtuyule.com/static/img/login/password.png" class="passwordLabel" mode=""></image>
					</view>
					<view class="input">
						<!-- <input type="password" placeholder-class="inputPla" placeholder="请输入密码" /> -->
						<input v-model="password" type="password" placeholder-class="inputPla" placeholder="请输入密码" />

					</view>
					<view class="forgetPassword" @tap="toForgetPwd">
						忘记?
					</view>
				</view>
				<view class="inputView loginButton" @tap="login">
					<text>登录</text>
				</view>
				<!-- <view class="inputView registerButton" @tap="toRegister">
					<text>注册</text>
				</view> -->
			</view>
		</view>

		<!-- agree -->
		<agree @agreeChange="handleAgreeChange"></agree>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import logoBack from "./components/logoBack.vue"
	import agree from "./components/agree.vue"

	export default {
		data() {
			return {
				agree: false, // 待修改，默认应该为false
				account: '', //待修改，默认应该为空13221010228
				password: '', //待修改，默认应该为空
				id: 0
			}
		},
		computed: {
			...mapState(['userInfo', 'loginInfo']),
		},
		components: {
			logoBack,
			agree
		},
		onShow() {
			if (this.userInfo.token !== undefined) {
				uni.reLaunch({
					url: '/pages/shouye/index'
				})
			}
		},
		onLoad(opts) {
			if (opts.id !== undefined) {
				this.id = opts.id
			}
			if (this.loginInfo.account !== undefined) {
				this.account = this.loginInfo.account
				this.password = this.loginInfo.password
			}
		},
		methods: {
			// 去注册页面
			toRegister() {
				uni.navigateTo({
					url: "/pages/login/register"
				})
			},
			// 去忘记密码页面
			toForgetPwd() {
				uni.navigateTo({
					url: "/pages/login/forgetPwd"
				})
			},
			// 获取agree的判断值
			handleAgreeChange(value) {
				this.agree = value;
			},
			// 登录事件
			async login() {
				if (!this.account) {
					uni.showToast({
						title: '请输入账号',
						icon: 'none'
					})
					return
				}
				if (!this.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
					return
				}
				if (!this.agree) {
					uni.showToast({
						title: '请先同意协议',
						icon: 'none'
					})
					return
				}
				let res = await this.$lib.$http.post({
					url: this.$lib.$urlMap.login,
					data: {
						account: this.account,
						password: this.password
					},
					needLogin: false
				})
				if (res.code == 1) {
					let info = res.data.userinfo
					let loginInfo = {
						account: this.account,
						password: this.password
					}
					this.$lib.$store.commit('setUserInfo', res.data.userinfo)
					this.$lib.$store.commit('setLoginStatus', 1)
					uni.showToast({
						title: '登录成功',
						icon: 'success',
						duration: 100
					})
					this.$lib.runTimer();
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/shouye/index'
						})
					}, 100)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1500
					})
				}
			}
		}
	}
</script>




<style lang="less" scoped>

	.form {
		display: flex;
		justify-content: center;
		padding-top:80rpx;

		.inputView {
			width: 600rpx;
			height: 100rpx;
			background-color: #F6F9FE;
			border-radius: 50rpx;
			margin-bottom: 35rpx;
			display: flex;
			position: relative;

			.label {
				padding-left: 41rpx;

				.accountLabel {
					padding-top: 28rpx;
					padding-bottom: 27rpx;
					width: 45rpx;
					height: 45rpx;
				}

				.passwordLabel {
					padding-top: 46rpx;
					padding-bottom: 31rpx;
					width: 47rpx;
					height: 23rpx;
				}
			}

			.input {
				// padding: 36rpx 0 34rpx 18rpx;
				padding-left: 18rpx;
				display: flex;
				align-items: center;
				font-size: 32rpx;
				// width: 70%;

				.inputPla {
					color: #AAAFB5;
					font-weight: 500;
				}

				/deep/ .inputPla {
					color: #AAAFB5;
					font-weight: 500;
				}
			}

			.forgetPassword {
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				right: 47rpx;
				font-size: 32rpx;
				color: #AAAFB5;
			}
		}

		.loginButton {
			background-color: #000000;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 32rpx;
		}

		.registerButton {
			background-color: #F6F9FE;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #419AFF;
		}
	}
</style>